<?php 
namespace FrontEnd\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Feed\Writer\Feed;
use Zend\View\Model\FeedModel;
use FrontEnd\Model\FrontEnd;

class RssController extends AbstractActionController
{
	protected $ProductTable;
	
	public function feedAction()
	{
		$feed = new Feed();
		$feed->setTitle('Feed Visiay');
		$feed->setFeedLink('http://visiay.com/rss', 'atom');
		$feed->addAuthor(array(
				'name'  => 'admin',
				'email' => 'contact@visiay.com',
				'uri'   => 'http://www.visiay.com',
		));
		$feed->setDescription('Current Feed Content');
		$feed->setLink('http://visiay.com');
		$feed->setDateModified(time());

		//fake data...
		$urlhientai = $this->request->getRequestUri();
		$mahoa = md5($urlhientai);
		$data = $this->getProductTable()->Rss($mahoa);
		$host = $this->getRequest()->getUri()->getHost();

		foreach($data as $row)
		{
			//create entry...
			$entry = $feed->createEntry();
			$entry->setTitle($row['NAME_PR']);
			$entry->setLink('http://'.$host.'/video/'.$row['ID_PR'].'-'.$this->vn2latin($row['NAME_PR'],true).'.html');

			$entry->setEnclosure(array(
                                  'type'   => 'image/jpeg',
                                  'uri'    => 'http://'.$host.'/public/template/upload/product/'.$row['LINKIMG'],
                                  'length' => '12389'
                             ));
			//$entry->setDateModified(strtotime($row['date_created']));
			$entry->setDateCreated(strtotime($row['DATE_PR']));
			
			$feed->addEntry($entry);
		}

		$feed->export('rss');

		$feedmodel = new FeedModel();
		$feedmodel->setFeed($feed);

		return $feedmodel;
	}
	
	
	public function getProductTable()
	{
		if (!$this->ProductTable) {
			$sm = $this->getServiceLocator();
			$this->ProductTable = $sm->get('Product\Model\ProductTable');
		}
		return $this->ProductTable;
	}
	
	function vn2latin($str, $tolower = false)
	{
		$str = preg_replace("/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/", 'a', $str);
		$str = preg_replace("/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/", 'e', $str);
		$str = preg_replace("/(ì|í|ị|ỉ|ĩ)/", 'i', $str);
		$str = preg_replace("/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/", 'o', $str);
		$str = preg_replace("/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/", 'u', $str);
		$str = preg_replace("/(ỳ|ý|ỵ|ỷ|ỹ)/", 'y', $str);
		$str = preg_replace("/(đ)/", 'd', $str);
		$str = preg_replace("/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/", 'A', $str);
		$str = preg_replace("/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/", 'E', $str);
		$str = preg_replace("/(Ì|Í|Ị|Ỉ|Ĩ)/", 'I', $str);
		$str = preg_replace("/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/", 'O', $str);
		$str = preg_replace("/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/", 'U', $str);
		$str = preg_replace("/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/", 'Y', $str);
		$str = preg_replace("/(Đ)/", 'D', $str);
		$str = preg_replace(array('/[^a-zA-Z0-9 -]/', '/[ -]+/', '/^-|-$/'),
				array('', '-', ''), $str);
		if ($tolower) {
			return strtolower($str);
		}
	
		return $str;
	
	}
}